package model;

import gui.NetworkView;

import java.util.ArrayList;
public class ModelViewLinker {

	private ArrayList<NetworkModel> models;
	private ArrayList<NetworkView> views;
	private static ModelViewLinker singleton;
	
	private ModelViewLinker() {
		models = new ArrayList<NetworkModel>();
		views = new ArrayList<NetworkView>();
	}
	
	public static ModelViewLinker getSingleton() {
		if(singleton == null) {
			singleton = new ModelViewLinker();
		}
		return singleton;
	}
	
	public void addModel(NetworkModel model) {
		if(models.indexOf(model) == -1) {
			models.add(model);
		}
	}
	
	public NetworkModel getModel(String filename) {
		for(int i = 0; i < models.size(); i++) {
			if(models.get(i).getFileName().equals(filename)) {
				return models.get(i);
			}
		}
		return null;
	}
	
	public void removeModel(NetworkModel model) {
		models.remove(model);
	}
	
	public void addView(NetworkView view) {
		views.add(view);
	}
	
	public void removeView(NetworkModel model, NetworkView view) {
		views.remove(view);
		if(model != null) {
			model.removeView(view);
		}
	}
	
	public int numberOfViews() {
		return views.size();
	}

	public void updateModel(NetworkModel oldModel, NetworkModel newModel) {
		models.remove(oldModel);
		for(int i = 0; i < views.size(); i++) {
			if(views.get(i).getModel() == oldModel) {
				views.get(i).setModel(newModel);
				views.get(i).updateView();
			}
		}
		addModel(newModel);
	}
}
